package com.aura.blog.service.impl;

import com.aura.blog.dto.request.AddBlogRequest;
import com.aura.blog.dto.request.UpdateBlogRequest;
import com.aura.blog.dto.response.BlogInfoResponse;
import com.aura.blog.entity.BlogInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.aura.blog.common.constant.Constants;
import com.aura.blog.common.exception.BlogException;
import com.aura.blog.common.util.BeanTransUtils;
import com.aura.blog.mapper.BlogInfoMapper;
import com.aura.blog.service.BlogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl implements BlogService {
    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    /**
     * 获取博客列表
     * 查询所有未删除的博客，并按更新时间倒序排列
     * 
     * @return 博客信息响应列表
     */
    @Override
    public List<BlogInfoResponse> getList() {
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(
                new LambdaQueryWrapper<BlogInfo>().eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE)
                        .orderByDesc(BlogInfo::getUpdateTime)
        );
        return blogInfos.stream().map(BeanTransUtils::trans)
                .collect(Collectors.toList());
    }

    /**
     * 获取博客详情
     * 
     * @param blogId 博客ID
     * @return 博客信息响应对象
     */
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        return BeanTransUtils.trans(getBlogInfo(blogId));
    }

    /**
     * 根据博客ID获取博客信息
     * 
     * @param blogId 博客ID
     * @return 博客信息对象
     */
    @Override
    public BlogInfo getBlogInfo(Integer blogId){
        return blogInfoMapper.selectOne(
                new LambdaQueryWrapper<BlogInfo>().eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE)
                        .eq(BlogInfo::getId, blogId));
    }

    /**
     * 添加博客
     * 
     * @param addBlogRequest 添加博客请求对象
     * @return 是否添加成功
     */
    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = BeanTransUtils.transBlogInfo(addBlogRequest);
        try {
            blogInfoMapper.insert(blogInfo);
            return true;
        }catch (Exception e){
            throw new BlogException("博客发布失败");
        }
    }

    /**
     * 更新博客
     * 
     * @param updateBlogRequest 更新博客请求对象
     * @return 是否更新成功
     */
    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = BeanTransUtils.transBlogInfo(updateBlogRequest);
        try {
            int i = blogInfoMapper.updateById(blogInfo);
            return i==1;
        }catch (Exception e){
            throw new BlogException("博客更新失败");
        }
    }

    /**
     * 删除博客（逻辑删除）
     * 
     * @param blogId 博客ID
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.IS_DELETE);
        try {
            int i = blogInfoMapper.updateById(blogInfo);
            return i==1;
        }catch (Exception e){
            throw new BlogException("博客更新失败");
        }
    }
    
    /**
     * 根据用户ID获取该用户的所有博客列表
     * 
     * @param userId 用户ID
     * @return 博客信息响应列表
     */
    @Override
    public List<BlogInfoResponse> getBlogsByUserId(Integer userId) {
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(
                new LambdaQueryWrapper<BlogInfo>()
                        .eq(BlogInfo::getUserId, userId)
                        .eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE)
                        .orderByDesc(BlogInfo::getUpdateTime)
        );
        return blogInfos.stream().map(BeanTransUtils::trans)
                .collect(Collectors.toList());
    }
    
    /**
     * 搜索博客（根据标题关键字）
     * 
     * @param keyword 搜索关键字
     * @return 博客信息响应列表
     */
    @Override
    public List<BlogInfoResponse> searchBlogs(String keyword) {
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(
                new LambdaQueryWrapper<BlogInfo>()
                        .like(BlogInfo::getTitle, keyword)
                        .eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE)
                        .orderByDesc(BlogInfo::getUpdateTime)
        );
        return blogInfos.stream().map(BeanTransUtils::trans)
                .collect(Collectors.toList());
    }
}